package com.xyd.auth.contoller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyd.auth.service.ISysRoleService;
import com.xyd.common.exception.YiYunGlobalException;
import com.xyd.common.result.GlobalResultClass;
import com.xyd.model.system.SysRole;
import com.xyd.model.system.vo.AssignRoleVO;
import com.xyd.model.system.vo.SysRoleQueryVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Tag(name="角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
@Slf4j
public class SysRoleController {
    @Autowired
    private ISysRoleService sysRoleService;
    // @PreAuthorize 指定权限标识符
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @Operation(summary = "获取所有角色列表")
    @GetMapping("/findAll")
    public GlobalResultClass<List<SysRole>> findAll(){
//        try {
//            // 自造异常
//            int i = 6/0;
//        }catch (Exception e){
//            throw new YiYunGlobalException(4000,"服务器异常");
//        }


        List<SysRole> list = sysRoleService.list();
        return  GlobalResultClass.ok(list);
    }
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @Operation(summary = "条件分页查询")
    @GetMapping("/{page}/{limit}")
    public GlobalResultClass<IPage<SysRole>>
    pageQeruyRole(@PathVariable Long page, @PathVariable Long limit,SysRoleQueryVO sysRoleQueryVO){
        log.info("模糊查询条件：",sysRoleQueryVO.getRoleName());
        // 查询总记录数数
        //调用service的方法实现
        //1 创建Page对象，传递分页相关参数
        //page 当前页 limit 每页显示记录数
        Page<SysRole> sysRolePage = new Page<>(page,limit);

        //2 封装条件，判断条件是否为空，不为空进行封装
        LambdaQueryWrapper<SysRole> sysRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        String roleName = sysRoleQueryVO.getRoleName();
        if(!StringUtils.isEmpty(roleName)){
             sysRoleLambdaQueryWrapper.like(SysRole::getRoleName, roleName);
        }
        //3 调用方法实现
        IPage<SysRole> page1 = sysRoleService.page(sysRolePage,sysRoleLambdaQueryWrapper);
        return GlobalResultClass.ok(page1);
    }
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @Operation(summary = "通过Id获取角色")
    @GetMapping("/get/{id}")
    public GlobalResultClass<SysRole>
    getSysRoleById(@PathVariable Long id){
        SysRole sysRole = sysRoleService.getById(id);
        return GlobalResultClass.ok(sysRole);
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @Operation(summary = "新增角色")
    @PostMapping(value = "/save")
    public GlobalResultClass saveSysRole(@RequestBody SysRole sysRole){
        log.info("传入得角色：{}",sysRole);
        sysRoleService.save(sysRole);
        return GlobalResultClass.ok();
    }
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @Operation(summary = "修改角色")
    @PutMapping ("/update")
    public GlobalResultClass updateSysRole(@RequestBody SysRole sysRole){
        sysRoleService.updateById(sysRole);
        return GlobalResultClass.ok();
    }
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @Operation(summary = "删除角色")
    @DeleteMapping("/remove/{id}")
    public GlobalResultClass removeSysRoleById(@PathVariable Long id){
        sysRoleService.removeById(id);
        return GlobalResultClass.ok();
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @Operation(summary = "批量删除")
    @DeleteMapping("/batchRemove")
    public GlobalResultClass removeSysRoleByIds(@RequestBody @Param("idList") Integer[] ids){
        sysRoleService.removeBatchByIds(Arrays.asList(ids));
        return GlobalResultClass.ok();
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @Operation(summary = "根据用户id获取角色")
    @GetMapping("/toAssign/{userId}")
    public GlobalResultClass toAssign(@PathVariable Long userId){
        Map<String, Object> map = sysRoleService.findRoleByUserId(userId);
        return GlobalResultClass.ok(map);
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.assignAuth')")
    @Operation(summary = "分配角色")
    @PostMapping("/doAssign")
    public GlobalResultClass doAssign(@RequestBody AssignRoleVO assignRoleVO){
        sysRoleService.doAssignRole(assignRoleVO);
        return GlobalResultClass.ok();
    }







}
